package cn.lutu.lutu.controller;

import cn.lutu.lutu.pojo.dto.CartDeleteDTO;
import cn.lutu.lutu.pojo.dto.CartInsertDTO;
import cn.lutu.lutu.pojo.entity.Cart;
import cn.lutu.lutu.response.ResultVO;
import cn.lutu.lutu.security.CustomUserDetails;
import cn.lutu.lutu.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * @date 2023-06-03
 * @author luoyinhan
 * */
@Slf4j
@Api(tags = "购物车模块")
@RestController
@RequestMapping("/v1/carts/")
public class CartController {
    @Autowired
    CartService cartService;

    @PostMapping("insert")
    @ApiOperation("添加商品至购物车")
    public ResultVO addProductToCart(@RequestBody CartInsertDTO cartInsertDTO, @AuthenticationPrincipal CustomUserDetails ud){
        System.out.println("cartInsertDTO = " + cartInsertDTO);
        cartService.addProductToCart(cartInsertDTO,ud.getId());
        return ResultVO.ok();
    }

    @PostMapping("{id}/delete")
    @ApiOperation("删除购物车商品")
    public ResultVO deleteProductFromCart(@PathVariable Long id){
        System.out.println("id = " + id);
        cartService.deleteProduct(id);
        return ResultVO.ok();
    }

    @GetMapping("selectAll")
    @ApiOperation("查看购物车所有商品")
    public ResultVO productListInCart(@AuthenticationPrincipal CustomUserDetails ud){
        return ResultVO.ok(cartService.cartList(Long.valueOf(1)));
    }

    @GetMapping("{categoryId}/search")
    public ResultVO searchByCategoryIdAndKeyword(@PathVariable Long categoryId,@RequestParam String keyword,@AuthenticationPrincipal CustomUserDetails ud){
        return ResultVO.ok(cartService.searchByCategoryAndKeyword(categoryId,keyword,Long.valueOf(1)));
    }

}
